package command

import (
	"os"
	"strconv"

	"xemo.top/docker/cli/types"
)

// DockerCliOption applies a modification on a DockerCli.
// DockerCliOption对DockerCli进行了修改。
type DockerCliOption func(cli *DockerCli) error

// WithContentTrustFromEnv 通过环境变量DOCKER_content_RUST值在cli上启用内容信任。
// WithContentTrustFromEnv enables content trust on a cli from environment variable DOCKER_CONTENT_TRUST value.
func WithContentTrustFromEnv() DockerCliOption {
	return func(cli *DockerCli) error {
		cli.contentTrust = false
		if e := os.Getenv("DOCKER_CONTENT_TRUST"); e != "" {
			if t, err := strconv.ParseBool(e); t || err != nil {
				//将任何其他值视为true
				// treat any other value as true
				cli.contentTrust = true
			}
		}
		return nil
	}
}

// WithContainerizedClient 在cli上设置容器化客户端构造函数。
// WithContainerizedClient sets the containerized client constructor on a cli.
func WithContainerizedClient(containerizedFn func(string) (types.ContainerizedClient, error)) DockerCliOption {
	return func(cli *DockerCli) error {
		cli.newContainerizeClient = containerizedFn
		return nil
	}
}
